#!/usr/bin/env node

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { FastMcpServer } from "./helper.mjs"
// import * as cheerio from "cheerio";
import { z } from "zod";


// ---- Initialize MCP Server ----
const mcp = new FastMcpServer({
  name: "MathServer",
  log_level: "ERROR",
  version: "1.0.0",
});


// ---- Tool 1: add ----
mcp.tool(
  "add",    // the name of function
  "Add two numbers",   // description
  { a: z.number(), b: z.number() },   // notations of the arguments
  async ({ a, b }) => (a+b)
);

// ---- Tool 2: sub ----
mcp.tool(
  "sub",    // the name of function
  "The difference between two numbers",   // description
  { a: z.number(), b: z.number() },   // notations of the arguments
  async ({ a, b }) => (a-b)
);

// ---- Tool 3: multiply ----
mcp.tool(
  "multiply",
  "Multiply two numbers",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => (a*b)
);

// ---- Tool 3: search_baidu ----
// mcp.tool(
//   "search_baidu",
//   "Search a query on Baidu and return the page title and canonical URL",
//   { query: z.string() },
//   async ({ query }) => {
//     const url = `https://www.baidu.com/s?wd=${encodeURIComponent(query)}`;
//     const response = await fetch(url);
//     const html = await response.text();
//     const $ = cheerio.load(html);

//     const title = "" + $("title").text() || "(no title)";
//     const canonical = $('link[rel="canonical"]').attr("href") || url;

//     const result = `title: ${title}\nurl: ${canonical}`;
//     return { content: [{ type: "text", text: result }] };
//   }
// );

const transport = new StdioServerTransport();
await mcp.connect(transport);

